<?php
/**
 * User: i@fedax.cn
 * Time: 2018/10/11 12:11
 */

namespace app\admin\model;


class GiftRecord extends DataBase
{
    protected $name = "gift_record";
    protected $autoWriteTimestamp =  true;

    public function getDataList($page_config,$where)
    {
        $data=$this
            ->alias('a')
            ->field('a.id,b.nickname,sum(a.gift_price) as wealth')
            ->join('users b','a.send_user_id = b.id','left')
            ->where($where)
            ->group('a.send_user_id')
            ->order('wealth desc')
            ->paginate($page_config);
        return $data;
    }

    /**
     * @return array
     * @user shuaishuai wu
     * @date 2018/12/13
     * @time 17:15
     * @title 预审家族活跃度信息
     */
    public function familyActiveInfoData()
    {
        $familyId = input('get.familyId');
        $queryTime = input('get.queryTime');
        if(!empty($queryTime) && $queryTime != date('Y-m')){
            if(strtotime($queryTime) > time()){
                return ['resultCode'=>110,'resultData'=>null,'resultDes'=>'查询结果集为空！'];
            }
            else{
                $startTime = strtotime($queryTime.'-01');
                $endTime = strtotime(date('Y-m-d',strtotime("$queryTime + 1 month - 1 day")).' 23:59:59');
            }
        }
        else{
            $startTime = strtotime(date('Y-m').'-01');
            $endTime = time();
        }
        $dayNum = ceil(($endTime - $startTime) / (3600 * 24));
        $data = [];
        for($i = 0;$i < $dayNum;$i++){

            $arr['date'] = date('Y-m-d',$startTime + 3600 * 24 * $i);
            $gift_price = $this->where([['send_family_id','=',$familyId],['create_time','between',[strtotime($arr['date']),strtotime($arr['date'].' 23:59:59')]]])->sum('gift_price');
            $arr['diamonds'] = $gift_price * 0.1;
            $data[] = $arr;

        }
        return ['resultCode'=>100,'resultData'=>$data,'resultDes'=>'查询成功！'];
    }

    /**
     * @user 帅shuaiwu
     * @date 2018/12/13
     * @time 17:42
     * @title 预审家族每日礼物详情
     */
    public function FamilyActiveGiftInfo()
    {
        $pageConfig = [
            'list_rows' => 25,
            'page' => input('get.page',1),
            'query' => [
                'familyId' => input('get.familyId'),
                'dates' => input('get.dates')
            ]
        ];
        $data =  $this->where([['send_family_id','=',input('get.familyId')],['create_time','between',[strtotime(input('get.dates')),strtotime(input('get.dates').' 23:59:59')]]])
            ->field('send_user_id,receive_user_id,gift_type,gift_price,num,create_time')->order('create_time DESC')->paginate($pageConfig);
        return $data;
    }

    /**
     * @return array
     * @user shuaishuai wu
     * @date 2018/12/13
     * @time 18:11
     * @title 预审家族小时活跃度
     */
    public function familyActiveHourInfo()
    {
        $times = strtotime(input('get.dates'));
        $data = [];
        for($i = 0;$i < 24;$i++){
            $arr['hours'] = $i.':00~'.($i + 1).':00';
            $gift_price = GiftRecord::where([['send_family_id','=',input('get.familyId')],['create_time','between',[$times + $i * 3600,$times + ($i + 1) * 3600]]])->sum('gift_price');
            $arr['diamonds'] = $gift_price * 0.1;
            $data[] = $arr;
        }
        return $data;
    }

    /**
     * @user shuaishuai wu
     * @date 2018/12/13
     * @time 19:00
     * @计算预审家族的活跃度
     */
    public function familyActiveCountInfo()
    {
        $start_time = strtotime(input('get.start_time'));
        if($start_time > time()) return ['code'=>-100,'msg'=>'你有毒吧'];
        $end_time = strtotime(input('get.end_time'));
        $familyId = input('get.familyId');

        $where[] = ['create_time','between',[$start_time,$end_time] ];
        $where[] = ['send_family_id','=',$familyId];

        $gift_price= $this->where($where)->sum('gift_price');
        $gift_price = $gift_price ==0? 0: $gift_price *0.1;

        return ['code'=>100,'data'=>$gift_price];
    }

    /**
     * @return array
     * @user shuaisuhai wu
     * @date 2018/12/13
     * @time 19:38
     * @title 自定义时间的房间流水
     */
    public function roomFlowingCount()
    {
        $roomId = input('get.roomId');
        $start_time = strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));


        if(empty($start_time) && empty($end_time)){

            //计算总流水（此处当心，本可以用room_id获取房间的总流水，但不准确，所以加一个时间戳辅助条件，有毒啊）
            $map[] = ['create_time','>',strtotime('1970-01-01 00:00:01') ];
            $map[] = ['room_id','=',$roomId ];
            $gift_price = $this->where($map)->sum('gift_price');
        }else{
            //自定义流水
            $map[] = ['create_time','between',[$start_time,$end_time] ];
            $map[] = ['room_id','=',$roomId ];

            $gift_price = $this->where($map)->sum('gift_price');
            $gift_price = $gift_price ==0? 0: $gift_price;

        }
        return ['code'=>100,'data'=>$gift_price];
    }
    /**
     * @return array|\PDOStatement|string|\think\Collection
     * @user 帅帅吴
     * @date 2018/12/24
     * @time 13:36
     * @title 自定义时间房间流水排行
     */
    public function roomFlowingRankData()
    {
        $start_time = strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));
        $limit = input('get.limit',10);

        if(!$start_time) $start_time=0;
        if(!$end_time) $end_time=time();

        $map[] = ['create_time', '>=',$start_time];
        $map[] = ['create_time', '<',$end_time];

        $data = $this->field('room_id,SUM(gift_price) as total')->where($map)->group('room_id')->order('total DESC')->limit($limit)->select()->toArray();

        foreach($data as $key => $value){

            $roomInfo = Room::field('r_id,r_name')->where('id','=',$value['room_id'])->find();
            $data[$key]['r_id'] =$roomInfo['r_id'];
            $data[$key]['r_name'] =$roomInfo['r_name'];
        }
        // halt($data);
        return ['code'=>100,'data'=>$data];

    }
    /**
     * @return array
     * @user 帅帅wu
     * @date 2018/12/13
     * @time 11:05
     * @title 家族总流水
     */
    public function familyFlowingCount()
    {
        $familyId = input('get.familyId');
        $start_time = strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));

        if(empty($start_time) && empty($end_time)){

            //计算总流水（此处当心，本可以用room_id获取房间的总流水，但不准确，所以加一个时间戳辅助条件，有毒啊）
            $map[] = ['create_time','>',strtotime('1970-01-01 00:00:01') ];
            $map[] = ['receive_family_id','=',$familyId ];
            $gift_price = $this->where($map)->sum('gift_price');
        }else{
            //自定义流水
            $map[] = ['create_time','between',[$start_time,$end_time] ];
            $map[] = ['receive_family_id','=',$familyId ];

            $gift_price = $this->where($map)->sum('gift_price');
            $gift_price = $gift_price ==0? 0: $gift_price;

        }
        return ['code'=>100,'data'=>$gift_price];
    }

    /**
     * @return array
     * @user shuaisugaiwu
     * @date 2018/12/24
     * @time 15:21
     * @title 家族流水排行榜
     */
    public function familyFlowingRankData()
    {
        $start_time = strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));
        $limit = input('get.limit',10);

        if(empty($start_time)) $start_time=0;
        if(empty($end_time)) $end_time=time();

        $map[] = ['create_time', '>=',$start_time];
        $map[] = ['create_time', '<',$end_time];

        $data = $this->field('receive_family_id,SUM(gift_price) as total')->where($map)->group('receive_family_id')->order('total DESC')->limit($limit)->select()->toArray();

        foreach($data as $key => $value){

            $familyDetail = Family::field('f_name,user_id')->where(['id'=>$value['receive_family_id'],'status'=>1,'type'=>1])->find();

            if(empty($familyDetail)){   //家族被禁用或未通过执行
                unset($data[$key]);
                continue;
            }

            $data[$key]['f_name'] = $familyDetail['f_name'];
            $userInfo = User::field('u_id,nickname')->where('id','=',$familyDetail['user_id'])->find();
            $data[$key]['u_id'] = $userInfo['u_id'];
            $data[$key]['nickname'] = $userInfo['nickname'];

        }
        //halt($data);
        return ['code'=>100,'data'=>$data];
    }

    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/12/24
     * @time 17:45
     * @title 个人房间流水排行榜
     */
    public function PersonalRoomFlowingRankData()
    {
        $start_time = strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));
        $limit = input('get.limit');

        if(!$start_time) $start_time=0;
        if(!$end_time) $end_time=time();

        $map[] = ['create_time', '>=',$start_time];
        $map[] = ['create_time', '<',$end_time];

        $Info = PersonalRoom::field('room_id')->select()->toArray();
        $roomIds = array_column($Info,'room_id');
        // halt($roomIds);
        $map[] = ['room_id','in',$roomIds];
        $data = $this->field('room_id,SUM(gift_price) as total')->where($map)->group('room_id')->order('total DESC')->limit($limit)->select()->toArray();
        // halt($data);
        foreach($data as $key => $value){

            $roomInfo = Room::field('r_id,r_name')->where('id','=',$value['room_id'])->find();
            $data[$key]['r_id'] = $roomInfo['r_id'];
            $data[$key]['r_name'] = $roomInfo['r_name'];
            $user_id = PersonalRoom::where('room_id','=',$value['room_id'])->value('user_id');
            $data[$key]['nickname'] = User::where('id','=',$user_id)->value('nickname');
        }
        return ['code'=>100,'data'=>$data];
    }

    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/12/24
     * @time 18:01
     * @title 家族房间流水排行榜
     */
    public function FamilyRoomFlowingRankData()
    {
        $start_time = strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));
        $limit = input('get.limit');

        if(!$start_time) $start_time=0;
        if(!$end_time) $end_time=time();

        $map[] = ['create_time', '>=',$start_time];
        $map[] = ['create_time', '<',$end_time];

        $Info = FamilyRoom::field('room_id')->select()->toArray();

        $roomIds = array_column($Info,'room_id');
        $map[] = ['room_id','in',$roomIds];

        $data = $this->field('room_id,SUM(gift_price) as total')->where($map)->group('room_id')->order('total DESC')->limit($limit)->select()->toArray();

        // halt($data);
        foreach($data as $key => $value){

            $family_id = FamilyRoom::where('room_id','=',$value['room_id'])->value('family_id');
            $familyDetail = Family::field('user_id,f_name')->where(['id'=>$family_id,'status'=>1,'type'=>1])->find();

            if(empty($familyDetail)){  //家族被禁用或未通过执行
                unset($data[$key]);
                continue;
            }

            $data[$key]['f_name'] = $familyDetail['f_name'];
            $userInfo = User::field('u_id,nickname')->where('id','=',$familyDetail['user_id'])->find();
            $data[$key]['u_id'] = $userInfo['u_id'];
            $data[$key]['nickname'] = $userInfo['nickname'];
            $roomInfo = Room::field('r_id,r_name')->where('id','=',$value['room_id'])->find();
            $data[$key]['r_id'] = $roomInfo['r_id'];
            $data[$key]['r_name'] = $roomInfo['r_name'];
        }
        array_merge($data);
        return ['code'=>100,'data'=>$data];
    }
}